package com.ruoyi.wlxz.controller;
import com.kennycason.kumo.WordFrequency;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wlxz.domain.Historychat;
import com.ruoyi.wlxz.domain.vo.ChatDateCount;
import com.ruoyi.wlxz.service.IHistorychatService;
import com.ruoyi.wlxz.service.StopWordsService;
import com.ruoyi.wlxz.util.WordCloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
 * 历史对话Controller
 *
 * @author zhanghao
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/wlxz/historychat")
public class HistorychatController extends BaseController {
    @Autowired
    private IHistorychatService historychatService;
    @Autowired
    private WordCloud wordCloud;
    @Autowired
    private StopWordsService stopWordsService;
    /**
     * 查询历史对话列表
     */
    //@PreAuthorize("@ss.hasPermi('wlxz:historychat:list')")
    @GetMapping("/list")
    public AjaxResult list(Historychat historychat) {
        //startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        historychat.setUserId(loginUser.getUserId());
        historychat.setIsDelete(0);
        List<Historychat> list = historychatService.selectHistorychatList(historychat);
        Map<String, List<Historychat>> categorizedChats = list.stream()
                .collect(Collectors.groupingBy(Historychat::getChatId));
        return AjaxResult.success(categorizedChats);
    }
    /**
     * 导出历史对话列表
     */
    @PreAuthorize("@ss.hasPermi('wlxz:historychat:export')")
    @Log(title = "历史对话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Historychat historychat) {
        List<Historychat> list = historychatService.selectHistorychatList(historychat);
        ExcelUtil<Historychat> util = new ExcelUtil<Historychat>(Historychat.class);
        util.exportExcel(response, list, "历史对话数据");
    }
    /**
     * 获取历史对话详细信息
     */
    @PreAuthorize("@ss.hasPermi('wlxz:historychat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(historychatService.selectHistorychatById(id));
    }
    /**
     * 新增历史对话
     */
    //@PreAuthorize("@ss.hasPermi('wlxz:historychat:add')")
    @Log(title = "历史对话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Historychat historychat) {
        historychat.setIsDelete(0);//0代表未删除
        historychat.setTime(ZonedDateTime.now(ZoneId.of("Asia/Shanghai")).toLocalDateTime());
        return toAjax(historychatService.insertHistorychat(historychat));
    }
    /**
     * 修改历史对话
     */
    @PreAuthorize("@ss.hasPermi('wlxz:historychat:edit')")
    @Log(title = "历史对话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Historychat historychat) {
        return toAjax(historychatService.updateHistorychat(historychat));
    }
    /**
     * 删除历史对话
     */
    @PreAuthorize("@ss.hasPermi('wlxz:historychat:remove')")
    @Log(title = "历史对话", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(historychatService.deleteHistorychatByIds(ids));
    }
    /**
     * 根据chatid,逻辑删除历史对话记录
     */
    //@PreAuthorize("@ss.hasPermi('wlxz:historychat:remove')")
    @Log(title = "历史对话", businessType = BusinessType.DELETE)
    @PutMapping("/{chatId}")
    public AjaxResult deleteByChatId(@PathVariable String chatId) {
        return toAjax(historychatService.deleteByChatId(chatId));
    }
    /**
     * 获取指定天数内的每日聊天统计
     */
    @GetMapping("/count")
    public AjaxResult getHistoryChatByData(@RequestParam("days") int days, @RequestParam("deptId") int deptId) {
        List<ChatDateCount> chatStats = historychatService.getHistoryChatByData(days, deptId);
        return AjaxResult.success(chatStats);
    }
    /**
     * 获取词云分析词
     * @param days
     * @return
     * @throws IOException
     */
    @GetMapping("/wordcloud")
    public AjaxResult getWordCloudByData(@RequestParam("days") int days) throws IOException {
        List<String> list = historychatService.getWordCloudByData(days);
        Set<String> stopWords = stopWordsService.getStopWords();
        List<WordFrequency> wordFrequencies = wordCloud.generateFromText(list, stopWords);
        return AjaxResult.success(wordFrequencies);
    }
}
